Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
ActiveRecord::RecordInvalid Exception: Validation failed: "Something" must exist

I am adding all is needed to create a post, but am getting an error message saying "Facility must exist." For some reason, the facility_id is capturing the name of the facility and not the ID. I am setting state for facilities, and mapping / filtering for my dropdown. Please see below:

function AddPostForm({ posts, handlePost})
{
  const [facilities, setFacilities] = useState ([])
  const [procedures, setProcedures] = useState ([])
  const uniques = procedures.map(procedure => procedure.procedure)
  .filter((value, index, self) => self.indexOf(value) === index )
// console.log("unique procedures:", uniques)
  const uniqFacility = facilities.map(facility => facility.name).filter((value,index, self) => self.indexOf(value) === index)

  useEffect(() => {
    fetch(`/posts/${procedures}`)
      .then((r) => r.json())
      .then(setProcedures);
  }, []);
  useEffect(() => {
    fetch("/facilities")
      .then((r) => r.json())
      .then(setFacilities);
  }, []);

  const [formData, setFormData] = useState({
    facility_id: "",
    procedure:'',
    date_of_procedure:'',
    date_of_invoice:'',
    patient_cost: "",
    insurance_cost: "",
    comments: ""
  })

  const { id } = useParams();

  function handleChange(event) {
    setFormData({
      ...formData,[event.target.name]: event.target.value,
    });
  }

  function handleSubmit(event) {
    event.preventDefault();
    handlePost(formData)

return (
    <div >
       <form onSubmit={handleSubmit}>
       <label htmlFor="facility_id">Facility:</label>
      <select
        id="facility_id"
        name="facility_id"
        value={formData.facility_id}
        onChange={handleChange}      >
        <option value="">Select Facility</option>
        {uniqFacility.map((facility) => (
          <option key={facility.id} value={facility.name}>
            {facility}
          </option>
        ))}
      </select>
 </form>
    </div>
  );
}
export default AddPostForm;
When I check params in the byebug I see facility_id=>"Name of Facility", and get an error message saying that Facility must exist. I tried adjusting the map / filter function to ...facility => facility.id).filter ... , which ends up giving me the facility_id and creates the post as if the name of the entity was its id. I think I need to adjust the map/filter formula, but I do not know how. Appreciate if someone help me out here.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda